<?php
/**
 * 
 */

class URI 
{
	private $configuri;//配置的uri
	private $requesturi;//请求的uri
	private $segment;//分割的uri数组
	
	public function __construct() {
		$this->configuri = config::BASEURL; 
		$this->_inituri();
	}
	
	/**
	 * 处理请求的uri
	 *
	 * '\','\\'转化为'/'
	 * config:BASEURL =>''
	 * 去掉url后面?及参数
	 * 
	 */
	protected  function _inituri() {
		$this->requesturi ='http://'. $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$this->requesturi = str_replace(array('\\','\\\\'), array('/'), $this->requesturi);
		$this->requesturi = str_replace(config::BASEURL,'',$this->requesturi);
		if($offset = strpos($this->requesturi,'?')) {
			 $this->requesturi = substr($this->requesturi,0,$offset);
		}
	}
	/**
	 * 分割uri
	 *
	 * @return 数组 
	 */
	
	public function segments() {
		$segments = explode('/',$this->requesturi);
		
		foreach($segments as $key=>$value) {
			if($value !== '' && $value !== 'index.php') {
				$this->segment[ ] = $value;
			}
		}
		return $this->segment;
	}
	
	public function getSeg($index) {
		$seg = $this->segments();

		if($index > count($seg)-1) return false;
		
		return $seg[$index];
	}
	
	public function redirect($url) {
		header("Location:".config::BASEURL.'index.php/'.$url);
	}
	public function refresh($url) {
		header("Location:refresh");
	}
}

?>
